package com.ilink.groundservice.mapper.study.course;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ilink.ilinkcommon.domain.PO.studyservice.StudyCourseFile;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface StudyCourseFileMapper extends BaseMapper<StudyCourseFile> {
    @Select("SELECT * FROM study_course_files WHERE courseFile_id = #{courseFileId}")
    StudyCourseFile getCourseFileById(@Param("courseFileId") Long courseFileId);

    @Select("SELECT * FROM study_course_files WHERE parent_id = #{parentId}")
    List<StudyCourseFile> getFilesByParentId(@Param("parentId") Long parentId);

    // 批量更新 sort_order
    @Update({
            "<script>",
            "UPDATE study_course_files",
            "SET sort_order = CASE courseFile_id",
            "<foreach collection='files' item='file' separator=' '>",
            "WHEN #{file.courseFileId} THEN #{file.sortOrder}",
            "</foreach>",
            "END",
            "WHERE courseFile_id IN",
            "<foreach collection='files' item='file' open='(' separator=',' close=')'>",
            "#{file.courseFileId}",
            "</foreach>",
            "</script>"
    })
    int updateSortOrderBatch(@Param("files") List<StudyCourseFile> files);

}
